package com.beehoo.dcportal.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.beehoo.dcportal.common.base.BaseException;

public class WriteFile {

	private File source;
	private File target;
	private int FILE_SIZE = 0;

	public WriteFile(File source, File target, int fileSize) {
		this.source = source;
		this.target = target;
		this.FILE_SIZE = fileSize;
	}

	public void write() throws BaseException {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(source), FILE_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(target), FILE_SIZE);
			byte[] image = new byte[FILE_SIZE];
			while (in.read(image) > 0) {
				out.write(image);
			}
		} catch (FileNotFoundException e) {
			throw new BaseException(e);
		} catch (IOException e) {
			throw new BaseException(e);
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				throw new BaseException(e);
			}
		}
	}
}
